using System.Collections.Generic;
using System.Threading;
using Oracle.ManagedDataAccess.Client;

namespace OracleInternal.Common
{
	internal class DeriveParamInfoPool
	{
		private object m_sync = new object();

		private long m_LastUsedCount;

		private Dictionary<string, DeriveParamInfo> m_cache;

		private int m_maxCacheSize;

		internal DeriveParamInfo this[string cmdText]
		{
			get
			{
				try
				{
					if (cmdText == null)
					{
						return null;
					}
					cmdText = cmdText.Trim();
					if (cmdText.Length == 0)
					{
						return null;
					}
					DeriveParamInfo value = null;
					if (m_cache.TryGetValue(cmdText, out value))
					{
						value.m_lastUsedCount = Interlocked.Increment(ref m_LastUsedCount);
						return value;
					}
					return null;
				}
				catch
				{
					return null;
				}
			}
			set
			{
				if (value == null || cmdText == null)
				{
					return;
				}
				cmdText = cmdText.Trim();
				if (cmdText.Length == 0 || m_cache.ContainsKey(cmdText))
				{
					return;
				}
				lock (m_sync)
				{
					if (!m_cache.ContainsKey(cmdText))
					{
						if (m_cache.Count >= m_maxCacheSize)
						{
							RemoveLRU();
						}
						value.m_lastUsedCount = ++m_LastUsedCount;
						m_cache.Add(cmdText, value);
					}
				}
			}
		}

		internal DeriveParamInfoPool(int maxCacheSize)
		{
			m_maxCacheSize = maxCacheSize;
			m_cache = new Dictionary<string, DeriveParamInfo>(m_maxCacheSize);
		}

		private void RemoveLRU()
		{
			string text = null;
			long num = long.MaxValue;
			foreach (KeyValuePair<string, DeriveParamInfo> item in m_cache)
			{
				if (item.Value.m_lastUsedCount < num)
				{
					num = item.Value.m_lastUsedCount;
					text = item.Key;
				}
			}
			if (text != null)
			{
				m_cache.Remove(text);
			}
		}
	}
}
